/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.session;

import bean.entity.Applicants;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import bean.entity.Interviews;

/**
 *
 * @author server
 */
@Stateless
@LocalBean
public class ApplicantSessionBean {

    @PersistenceContext(unitName = "RecruitmentProcessSystem-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    public boolean addNewApplicant(Applicants app) {
        try {
            em.persist(app);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public List<Applicants> findIdapplicant(String id) {
        List<Applicants> list = null;
        try {
            Query query = em.createNamedQuery("Applicants.findByIDApplicant");
            query.setParameter(1, "%" + id + "%");

            list = query.getResultList();
        } catch (NoResultException e) {
            return null;
        }
        return list;
    }

    public int updateStatus(List<Interviews> List) {
        int i = 0;
        try {
            for (Interviews inte : List) {
                if (inte.getInStatus() == 2) {
                    Query query = em.createNamedQuery("Applicants.updateStatus");
                    query.setParameter("status", 3);
                    query.setParameter("applicantID", inte.getApplicants().getApplicantID());
                    i = query.executeUpdate();
                } else if (inte.getInStatus() == 3) {
                    Query query = em.createNamedQuery("Applicants.updateStatus");
                    query.setParameter("status", 4);
                    query.setParameter("applicantID", inte.getApplicants().getApplicantID());
                    i = query.executeUpdate();
                }

            }
        } catch (Exception e) {
            return 0;
        }
        return i;
    }

    public int updateOneStatus(String id, int status) {
        int i = 0;
        try {
            if (status == 1) {//In Process
                Query query = em.createNamedQuery("Applicants.updateStatus");
                query.setParameter("status", 2);
                query.setParameter("applicantID", id);
                i = query.executeUpdate();
            }
            if (status == 3) { //Hired
                Query query = em.createNamedQuery("Applicants.updateStatus");
                query.setParameter("status", 3);
                query.setParameter("applicantID", id);
                i = query.executeUpdate();
            } else if (status == 4) { //Banned
                Query query = em.createNamedQuery("Applicants.updateStatus");
                query.setParameter("status", 4);
                query.setParameter("applicantID", id);
                i = query.executeUpdate();
            }
        } catch (Exception e) {
            return 0;
        }
        return i;
    }

    public List<Applicants> findApplicantName(String id) {
        List<Applicants> list = null;
        try {
            Query query = em.createNamedQuery("Applicants.findByNameApplicant");
            query.setParameter(1, "%" + id + "%");

            list = query.getResultList();
        } catch (NoResultException e) {
            return null;
        }
        return list;
    }

    public List<Applicants> findAllApplicant() {
        return em.createNamedQuery("Applicants.findAll").getResultList();
    }

    public Applicants findApplicantByID(String applicantID) {
        Applicants app = null;
        try {
            Query query = em.createNamedQuery("Applicants.findByApplicantID");
            query.setParameter("applicantID", applicantID);
            app = (Applicants) query.getSingleResult();
            return app;
        } catch (NoResultException e) {
            return null;
        }

    }

    public void updateApplicant(List<Applicants> List) {
        for (Applicants a : List) {
            em.merge(a);
        }
    }

    public int countStatusApplicants(int status) {
        int i = 0;
        try {
            i = ((Long) em.createNamedQuery("Applicants.countStatusApplicants").setParameter("status", status).getSingleResult()).intValue();
        } catch (NoResultException e) {
            return 0;
        }
        return i;
    }

    public List<Applicants> findByStatus(int status) {
        List<Applicants> list = null;
        try {
            Query query = em.createNamedQuery("Applicants.findStatus");
            query.setParameter("status", status);
            list = query.getResultList();
        } catch (NoResultException e) {
            return null;
        }
        return list;
    }
}
